// TypeScript file
class GameFightView extends egret.Sprite
{
    protected boshu: number = 0;
    protected totalEnemy: number = 0;
    protected curScene: number = 0;
    protected bg: BgView;
    protected enemyNum: any;
    protected targetName: string = "";
    protected totalEnemyNum: number = 0;
    protected oneToTwo: number = 0;
    protected showEnemyTime: number = 0;
    protected showResizeBtn: number = 0;
    protected timeBoo1: number = 0;
    protected timeBoo: number = 0;
    protected onLockNum: number = 4;
    protected onLockBtn: boolean;
    protected widthPoint: number = 0;
    protected freeTime: number = 0;
    protected isStart: boolean = false;
    protected isFire: boolean = false;
    protected streakWin: number = 0;
    protected btnY: number = 0;
    protected streakWinNum: StreakNum;
    protected stopPanduan: boolean = false;
    protected isShowTwoEnemy:boolean = false;
    protected showEnemyFunNum: number = 0;
    protected shanBoo: boolean = false;
    protected win: boolean = false;
    protected stopGame: boolean = false;
    protected isPlayDaZhao: boolean = false;
    protected dazhaoTime: number = 0;

    public redGirl: RedGirl;
    protected enemySp: egret.Sprite;
    protected bombSp: egret.Sprite;
    protected uiSp: egret.Sprite;
    protected gameOverSp: egret.Bitmap;
    protected houseSp: House;
    protected bloodBar: BloodBar;
    protected scoreView: ScoreView;
    protected girlHead: GirlDistanceBar;
    protected shan: egret.Bitmap;
    protected dazhaoBar: DaZhaoBar;
    protected dmask: egret.Bitmap;
    protected dazhaoMc: Line;

    protected thisF: Array<any> = ["1","0", ".", "0", ".", "4", ".", "1", "8", "0", ":", "3", "0", "0","0"];
    protected enemyFrameInfo: string = Const.setSwfArr.join("");
    protected popArr: Array<PromptPop> = [];
    protected dazhaoArr: Array<Line> = [];
    protected bombArr: Array<Bomb> = [];
    protected btnArr: Array<FightButton> = [];
    protected oneEnemyArr: Array<Enemy> = [];
    protected twoEnemyArr: Array<Enemy> = [];
    protected threeEnemyArr: Array<Enemy> = [];
    protected fourEnemyArr: Array<Enemy> = [];
    protected static allArr: Array<any> = [];

    protected b: number = 0;

    constructor()
    {
        super();
        GameData.bgSpeed = 3;
        this.totalEnemyNum = 100;
        this.boshu = 1;
        this.curScene = 1;
        this.showEnemyTime = 30;
        this.showResizeBtn = 50;
        this.totalEnemy = 0;
        GameData.enemySpeed = 10;
        this.freeTime = 5;
        this.oneToTwo = 20;
        GameData.redGirlDistance = 0;
        GameFightView.allArr = [this.oneEnemyArr, this.twoEnemyArr, this.threeEnemyArr, this.fourEnemyArr];

        this.initView();
        this.initLayer();
        this.initBomb();
        egret.Ticker.getInstance().register(this.onFrameHandler, this);
    }

    public initLayer(): void
    {
        this.dmask = ResourceUtils.createBitmapByName("mask_png");
        this.addChild(this.dmask);
        this.dmask.visible = false;
        let i: number = 0;
        let n: number = 10;
        for (let i: number = 0, n: number = 10; i < n; ++i)
        {
            this.dazhaoMc = new Line();
            this.addChild(this.dazhaoMc);
            this.dazhaoMc.y = -this.dazhaoMc.height;
            this.dazhaoMc.visible = false;
            this.dazhaoArr.push(this.dazhaoMc);
        }

        this.enemySp = new egret.Sprite();
        this.addChild(this.enemySp);
        this.uiSp = new egret.Sprite();
        this.addChild(this.uiSp);

        this.bombSp = new egret.Sprite();
        this.addChild(this.bombSp);

        this.shan = ResourceUtils.createBitmapByName("shan_png");
        this.addChild(this.shan);
        this.shan.visible = false;

        this.houseSp = new House();
        this.addChild(this.houseSp);
        this.houseSp.x = -66;
        this.houseSp.y = -this.houseSp.height;
        this.houseSp.visible = false;

        this.redGirl = new RedGirl();
        this.redGirl.x = Const.SENT_WIDTH / 2;
        this.redGirl.y = Const.SENT_HEIGHT - 50;
        this.addChild(this.redGirl);

        this.streakWinNum = new StreakNum();
        this.streakWinNum.x = Const.SENT_WIDTH/2 - this.streakWinNum.width;
        this.streakWinNum.y = 86;
        this.addChild(this.streakWinNum);
        this.streakWinNum.visible = false;

        for (let i: number = 0, n: number = 4; i < n; ++i)
        {
            let fightButton: FightButton = new FightButton();
            fightButton.touchEnabled = true;
            fightButton.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBegin, this);
            fightButton.addEventListener(egret.TouchEvent.TOUCH_END, this.onEnd, this);

            this.uiSp.addChild(fightButton);
            fightButton.x = i * (fightButton.width + 14) + 10;
            fightButton.y = 500;
            fightButton.name = i + "";
            this.widthPoint = fightButton.width / 2;
            this.btnY = fightButton.y + this.widthPoint * 2;
            this.btnArr.push(fightButton);
        }

        this.bloodBar = new BloodBar();
        this.uiSp.addChild(this.bloodBar);
        this.bloodBar.x = Const.SENT_WIDTH / 2 - this.bloodBar.width / 2 - 30;
        this.bloodBar.y = 30;
        this.bloodBar.scaleBlodX();

        this.scoreView = new ScoreView();
        this.scoreView.setValue(GameData.score);
        this.uiSp.addChild(this.scoreView);
        this.scoreView.x = Const.SENT_WIDTH / 2 * 1.2 - 40;
        this.scoreView.y = 5;

        this.girlHead = new GirlDistanceBar();
        this.uiSp.addChild(this.girlHead);
        this.girlHead.x = Const.SENT_WIDTH - 40;
        this.girlHead.y = Const.SENT_HEIGHT / 10 - 30;

        let fever: egret.Bitmap = ResourceUtils.createBitmapByName("fever_png");
        this.uiSp.addChild(fever);
        fever.x = this.bloodBar.x;
        fever.y = 5;

        this.dazhaoBar = new DaZhaoBar();
        this.uiSp.addChild(this.dazhaoBar);
        this.dazhaoBar.initView();
        this.dazhaoBar.x = fever.width + 5;
        this.dazhaoBar.y = 5;
        let prom: PromptPop;
        for (let i: number = 0, len: number = 10; i < len; ++i)
        {
            prom = new PromptPop();
            prom.activate(Const.SENT_WIDTH/2 - 100, Const.SENT_HEIGHT - 300, prom.config);
            this.addChild(prom);
            this.popArr.push(prom);
        }
    }

    public popProm(str: string = ""): void
    {
        for (let i: number = 0, len: number = this.popArr.length; i < len; ++i)
        {
            if (!this.popArr[i].targetMc.visible)
            {
                this.popArr[i].show(str);
                break;
            }
        }
    }

    public initBomb(): void
    {
        let bomb: Bomb;
        for (let i: number = 0, n: number = 10; i < n; ++i)
        {
            bomb = new Bomb();
            this.bombSp.addChild(bomb);
            this.bombArr.push(bomb);
        }

    }

    public initEnemy(type: number): void
    {

    }

    public initView(): void
    {
        this.bg = new BgView();
        this.addChild(this.bg);
        this.bg.initView(GameData.curScene);
    }

    public onFrameHandler(evt: egret.Event): void
    {
        if (GameData.isPause) return;
        this.bg.updata();
        
        if (this.win)
        {
            
            this.houseSp.y += GameData.bgSpeed;
            if (this.houseSp.y >= -80)
            {
                GameData.bgSpeed = 3;
                this.win = false;
                this.houseSp.y = -80;
            }
        }

        this.playDaZhao();

        if (GameData.dubleSorce)
        {
            ++GameData.curTimeNum;
            if (GameData.curTimeNum >= 300)
            {
                GameData.curTimeNum = 0;
                GameData.dubleSorce = false;
            }
        }

        if (GameData.sheDie)
        {
            ++GameData.sheTimeNum;
            if (GameData.sheTimeNum >= 400)
            {
                GameData.sheTimeNum = 0;
                GameData.sheDie = false;
            }
        }

        this.girlHead.moveHead(this.totalEnemyNum, this.freeTime);
        ++this.timeBoo;
        if (this.onLockBtn)
        {
            ++this.timeBoo1;
        }

        this.startGame();
        this.onResize();
    }

    public onResize(): void
    {

    }

    public startGame(): void
    {
        let streakWinNum: StreakNum = this.streakWinNum;
        
        if (0 == this.streakWin)
        {
            streakWinNum.visible = false;
        }
        else
        {
            streakWinNum.visible = true;
            streakWinNum.setValue(this.streakWin);
        }

        if (this.isStart)
        {
            this.enemyMoveOrStop(this.oneEnemyArr);
            this.enemyMoveOrStop(this.twoEnemyArr);
            this.enemyMoveOrStop(this.threeEnemyArr);
            this.enemyMoveOrStop(this.fourEnemyArr);
        }

        if (GameData.profectNum >= GameData.dazhaoTime)
        {
            this.isPlayDaZhao = true;
        }

        if (this.stopPanduan) return;

        if (this.shanBoo)
        {
            this.shan.visible = true;
            this.shan.alpha = 1;
            egret.Tween.get(this.shan).to({"alpha":0, "visible":false}, 300).call(this.shanFun, this);
        }

        if (this.timeBoo >= this.showEnemyTime)
        {
            this.initBoShu();
            if (!this.stopGame)
            {
                if (GameData.stopCreateEnemy == 0)
                {
                    this.createEnemy();
                }
                else
                {
                    GameData.stopCreateEnemy--;
                }
                this.isStart = true;
            }
        }
    }

    public initBoShu(): void
    {

    }

    public createEnemy(): void
    {

    }

    public onBegin(evt: egret.TouchEvent): void
    {
        if (this.onLockBtn) return;

        let curNum: number = <number>evt.currentTarget.name;
        this.fire(evt.currentTarget);
        this.hitTestObj(curNum, curNum);
        (<FightButton>evt.currentTarget).goPlay(1);

    }

    public fire(btn: FightButton): void
    {
        let len: number = this.bombArr.length;
        for (let i: number = 0; i < len; ++i)
        {
            let bomb: Bomb = this.bombArr[i];
            if (bomb.visible) continue;
            
            this.isFire = true;
            bomb.x = Const.SENT_WIDTH / 2;
            bomb.y = 750;
            bomb.lastX = btn.x + this.widthPoint;
            bomb.lastY = btn.y + this.widthPoint;
            bomb.move();
            bomb.visible = true;
            break;
        }
    }

    public hitTestObj(num: number, idx: number): void
    {
        let arr: Array<any> = GameFightView.allArr[idx];
        let btn: FightButton = this.btnArr[num];
        let len: number = arr.length;
        for (let i: number = 0; i < len; ++i)
        {
            let enemy: Enemy = arr[i];
            if (enemy.stopMove || enemy.isStopHasClick) continue;
            if (enemy.y < btn.y) continue;

            if (enemy.y > btn.y + this.widthPoint * 2) continue;

            if (this.targetName == enemy.name)
            {
                if (!(enemy.type == 3 || enemy.type == 5))
                {
                    break;
                }
            }
            this.targetName = enemy.name;
            this.bTitTextE(btn, enemy, arr, i);
            break;
        }
    }

    public lockBtnFuc(btn: FightButton): void
    {
        btn.goPlay(1);
        this.onLockNum = parseInt(btn.name);
        this.onLockBtn = true;
    }

    public onEnd(evt: egret.TouchEvent): void
    {
        let curNum: number = evt.currentTarget.name;
        if (curNum == this.onLockNum) return;
        let btn: FightButton = evt.currentTarget;
        btn.goPlay(0);
    }

    public bTitTextE(btn: FightButton, enemy: Enemy, arr: Array<any> = [], idx: number = 0): void
    {
        let eY: number = enemy.y;
        let bY: number = btn.y - this.widthPoint / 2;
        let circle: number = bY + this.widthPoint * 3;
        if (eY < bY)
        {
            this.popProm("pop3");
            SoundUtils.instance().play(SoundType.MINSSSOUND);
            this.lockBtnFuc(btn);
            return;
        }

        if (eY < circle)
        {
            if (eY >= (circle - this.widthPoint * 1.2) && eY < circle - this.widthPoint * 0.8)
            {
                this.hitFun(enemy, 1, arr, idx);
            }
            else if (eY >= circle - this.widthPoint * 1.6 && eY < circle - this.widthPoint * 1.2 || 
            eY >= circle - this.widthPoint * 0.8 && eY < circle - this.widthPoint * 0.4)
            {
                this.hitFun(enemy, 1, arr, idx);
            }
            else if (eY >= circle - this.widthPoint * 2 &&
            eY < circle - this.widthPoint * 1.6 ||
            eY >= circle - this.widthPoint * 0.4 &&
            eY < circle + this.widthPoint * 0.2)
            {
                this.hitFun(enemy, 2, arr, idx);
            }
            else
            {
                this.hitFun(enemy, 2, arr, idx);
            }
            return;
        }

        if (eY == circle)
        {
            return;
        }

        if (enemy.type == 3 || enemy.type == 5)
        {
            if (enemy.blod == 0)
            {
                enemy.guo = 1;
            }
        }
        else
        {
            enemy.guo = 1;
        }

        if (enemy.type == 1 || enemy.type == 3 || enemy.type == 5)
        {
            this.popProm("pop3");
            if (GameData.profectNum >= GameData.dazhaoTime)
            {
                this.isPlayDaZhao = true;
                return;
            }
            SoundUtils.instance().play(SoundType.MINSSSOUND);

            this.streakWin = 0;
            GameData.profectNum = 0;
            this.dazhaoBar.setValue();
            this.shanBoo = true;
            --GameData.blood;
            SoundUtils.instance().play(SoundType.BEHITSOUND);
            this.bloodBar.scaleBlodX();
        }

        if (GameData.blood <= 0)
        {
            this.gameOver();
        }
    }

    public hitFun(enemy: Enemy, num: number = 0, arr: Array<any> = [], idx: number = 0): void
    {
        if (GameData.dubleSorce)
        {
            num = 1;
        }

        SoundUtils.instance().play(SoundType.HITSOUND);
        switch (enemy.type)
        {
            case 1:
            case 3:
            case 5:
            {
                if (num == 2)
                {
                    GameData.score += 30;
                    GameData.profectNum += 0.5;
                    this.popProm("pop2");
                }
                else if (num == 1)
                {
                    GameData.score += 50;
                    GameData.profectNum += 1;
                }
                this.scoreView.setValue(GameData.score);
                if (GameData.profectNum < GameData.dazhaoTime)
                {
                    this.dazhaoBar.setValue();
                }
            }
            break;
        }

        this.streakWin++;
        this.hitOver(enemy, arr, idx);
    }

    public enemyMoveOrStop(arr: Array<Enemy>): void
    {
        if (arr.length == 0) return;

        for (let i: number = arr.length - 1, n: number = 0; i >= n; --i)
        {
            let enemy: Enemy = arr[i];
            if (enemy.over) continue;

            enemy.move();

            if (enemy.x < -enemy.width || enemy.x > Const.SENT_WIDTH + enemy.width / 2)
            {
                enemy.over = true;
            }

            if (enemy.over)
            {
                enemy.dispose();
                this.enemySp.removeChild(enemy);
                arr.splice(i, 1);
            }

            if (GameData.blood <= 0)
            {
                this.gameOver();
            }

            if (arr.length == 0) return;
            enemy = arr[i];
            if (enemy == null || enemy == undefined) continue;

            if (enemy.y < this.btnY - this.widthPoint * 1.5) continue;

            if (enemy.guo != 0) continue;

            if (this.btnY + this.widthPoint / 2 >= enemy.y) continue;

            if (!(enemy.type == 1 || enemy.type == 3 || enemy.type == 5)) continue;

            if (GameData.profectNum >= GameData.dazhaoTime)
            {
                this.isPlayDaZhao = true;
                return;
            }

            this.streakWin = 0;
            enemy.guo = 1;
            //enemy.over = true;
            GameData.profectNum = 0;
            this.dazhaoBar.setValue();
            --GameData.blood;
            SoundUtils.instance().play(SoundType.BEHITSOUND);
            this.shanBoo = true;
            this.bloodBar.scaleBlodX();

       }

    }

    public isSetEnemyFrame(): string
    {
        return null;
    }

    public gameWin(): void
    {
        GameData.isWin = true;
        this.onLockBtn = true;
        this.houseSp.visible = true;
        this.stopPanduan = true;
        GameData.bgSpeed = 6;
        this.win = true;
        this.dazhaoBar.visible = false;
        this.scoreView.visible = false;
        this.bloodBar.visible = false;
        this.girlHead.visible = false;
        let btn: FightButton;
        for (let i: number = 0, len: number = this.btnArr.length; i < len; ++i)
        {
            btn = this.btnArr[i];
            btn.visible = false;
            btn.touchEnabled = false;
            btn.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBegin, this);
            btn.removeEventListener(egret.TouchEvent.TOUCH_END, this.onEnd, this);     
        }

        egret.Tween.get(this.redGirl).wait(1000).to({y:240}, 1500).call(this.func1, this);
    }

    private func1(): void
    {
        this.redGirl.gotoWin();
        egret.Tween.get(this.redGirl).to({y:230}, 1500).call(this.aaa, this);
    }

    private aaa(): void
    {
        GameData.count = 0;
        GameData.profectNum = 0;
        this.dazhaoBar.setValue();
        GameData.stopCreateEnemy = 0;
        GameData.redGirlDistance = 0;

        let win: egret.Bitmap = ResourceUtils.createBitmapByName("complete_png");
        this.addChild(win);
        let gw: number = Const.SENT_WIDTH / 2 - win.width / 2;
        let gh: number = Const.SENT_HEIGHT / 2 - win.height / 2;
        win.scaleX = win.scaleY = 2;
        win.alpha = 0;
        win.x = Const.SENT_WIDTH / 2 - win.width;
        win.y = Const.SENT_HEIGHT / 2 - win.height;

        SoundUtils.instance().stop(SoundType.BGSOUND);
        SoundUtils.instance().play(SoundType.WINSOUND);

        egret.Tween.get(win).to(
        {
            scaleX: 1,
            scaleY: 1,
            x: gw,
            y: gh,
            alpha: 1
        }, 500)
        .call(this.winaaa, this);
    }

    private winaaa(): void
    {
        egret.setTimeout(this.over.bind(this), this, 1500);
    }

    public over(): void
    {

    }

    public gameOver(): void
    {
        GameData.isWin = false;
        this.onLockBtn = true;
        GameData.isPause = true;
        GameData.count = 0;
        GameData.profectNum = 0;
        GameData.stopCreateEnemy = 0;
        GameData.redGirlDistance = 0;
        this.isStart = false;

        this.gameOverSp = ResourceUtils.createBitmapByName("gameOver_png");
        this.addChild(this.gameOverSp);
        let gw: number = Const.SENT_WIDTH / 2 - this.gameOverSp.width / 2;
        let gh: number = Const.SENT_HEIGHT / 2 - this.gameOverSp.height / 2;
        this.gameOverSp.scaleX = this.gameOverSp.scaleY = 2;
        this.gameOverSp.alpha = 1;
        this.gameOverSp.x = Const.SENT_WIDTH / 2 - this.gameOverSp.width;
        this.gameOverSp.y = Const.SENT_HEIGHT / 2 - this.gameOverSp.height;

        SoundUtils.instance().stop(SoundType.BGSOUND);
        SoundUtils.instance().play(SoundType.OVERSOUND);

        egret.Tween.get(this.gameOverSp).to({scaleX:1, scaleY:1, x:gw, y:gh, alpha:1}, 500).call(this.bbb, this);
    }

    private bbb(): void
    {
        egret.setTimeout(this.overbbb.bind(this), this, 1500);
    }

    private overbbb(): void
    {
        GameSceneView.gameScene.over();
        egret.Tween.removeAllTweens();
        this.dispose();
    }

    public dispose(): void
    {
        egret.Ticker.getInstance().unregister(this.onFrameHandler, this);
        this.oneEnemyArr = [];
        this.popArr = [];
        this.twoEnemyArr = [];
        this.threeEnemyArr = [];
        this.fourEnemyArr = [];
        this.btnArr = [];
        this.enemySp = null;
        this.uiSp = null;
        this.bg = null;
        this.redGirl = null;
        this.houseSp = null;
    }

    public hitOver(e: Enemy, arr: Array<any> = [], idx: number = 0): void
    {

    }

    private shanFun(): void
    {
        this.shanBoo = false;
    }

    private eorrror(): void
    {

    }

    private playDaZhao(): void
    {
        if (!this.isPlayDaZhao) return;

        this.dmask.visible = true;
        ++this.b;
        ++this.dazhaoTime;

        if (10 <= this.b)
        {
            for (let i: number = 0, n: number = this.dazhaoArr.length; i < n; ++i)
            {
                let dazhao: Line = this.dazhaoArr[i];
                if (dazhao.visible == false)
                {
                    dazhao.y = -dazhao.height;
                    dazhao.x = this.btnArr[Math.floor(Math.random() * 4 + 1) - 1].x;
                    dazhao.move();
                    break;
                }
            }
            this.b = 0;
        }

        this.dazhaoBar.boo = true;
        this.dazhaoBar.rx = ((400 - this.dazhaoTime) / 400) * this.dazhaoBar.w - this.dazhaoBar.w;

        if (this.dazhaoTime > 400)
        {
            this.dazhaoBar.boo = false;
            this.dmask.visible = false;
            this.dazhaoTime = 0;
            this.isPlayDaZhao = false;
            GameData.profectNum = 0;
        }

    }
}